using UnityEngine;

public class PlayerManager : MonoBehaviour
{
	[HideInInspector]
	public CharacterSystem PlayingCharacter;

	public float AutoRespawnDelay = 3f;

	public bool AutoRespawn;

	public bool AskForRespawn = true;

	public float SaveInterval = 5f;

	public bool SavePlayer = true;

	public bool SaveToServer;

	[HideInInspector]
	public SpectreCamera Spectre;

	private float timeTemp;

	private bool savePlayerTemp;

	private float timeAlivetmp;

	private bool autoRespawntmp;

	private bool askForRespawntmp = true;

	[HideInInspector]
	public bool spectreMode;

	[HideInInspector]
	public float respawnTimer;

	private void Start()
	{
		savePlayerTemp = SavePlayer;
		autoRespawntmp = AutoRespawn;
		askForRespawntmp = AskForRespawn;
	}

	public void Reset()
	{
		SavePlayer = savePlayerTemp;
		AutoRespawn = autoRespawntmp;
		AskForRespawn = askForRespawntmp;
		spectreMode = false;
	}

	private void Update()
	{
		if (!UnitZ.gameNetwork.IsDedicatedServer && UnitZ.gameManager.IsPlaying)
		{
			OnPlaying();
		}
	}

	public void Respawn(int spawner)
	{
		if ((bool)PlayingCharacter && !PlayingCharacter.IsAlive)
		{
			PlayingCharacter.ReSpawn(spawner);
		}
	}

	public void Respawn(byte team, int spawner)
	{
		if ((bool)PlayingCharacter && !PlayingCharacter.IsAlive)
		{
			PlayingCharacter.ReSpawn(team, spawner);
		}
	}

	public void SpectreMode(bool enable)
	{
		spectreMode = enable;
		if ((bool)Spectre)
		{
			Spectre.enabled = !enable;
			FPSFlyCamera component = Spectre.GetComponent<FPSFlyCamera>();
			if ((bool)component)
			{
				component.enabled = enable;
			}
			if (UnitZ.Hud.IsPanelOpened("Lose"))
			{
				UnitZ.Hud.ClosePanelByName("Lose");
			}
		}
	}

	public void OnPlaying()
	{
		if ((bool)PlayingCharacter)
		{
			if ((bool)UnitZ.playerSave && PlayingCharacter.IsAlive)
			{
				if (Time.time >= timeTemp + SaveInterval)
				{
					timeTemp = Time.time;
					if (SavePlayer)
					{
						PlayingCharacter.SaveCharacterData(SaveToServer);
					}
				}
				if (UnitZ.Hud.IsPanelOpened("Lose"))
				{
					UnitZ.Hud.ClosePanelByName("Lose");
				}
				timeAlivetmp = Time.time;
			}
			if (!PlayingCharacter.IsAlive)
			{
				if (AutoRespawn)
				{
					respawnTimer = timeAlivetmp + AutoRespawnDelay - Time.time;
					if (Time.time > timeAlivetmp + AutoRespawnDelay)
					{
						Respawn(-1);
						UnitZ.Hud.ClosePanelByName("Lose");
						UnityEngine.Debug.Log("Chaacter respawned (" + Time.timeSinceLevelLoad + ")");
					}
				}
				else if (!UnitZ.Hud.IsPanelOpened("Lose") && !spectreMode)
				{
					UnitZ.Hud.OpenPanelByName("Lose");
				}
			}
		}
		else
		{
			findPlayerCharacter();
			if (!(PlayingCharacter == null))
			{
			}
		}
		if (Spectre != null && (bool)PlayingCharacter)
		{
			if (!PlayingCharacter.IsAlive)
			{
				Spectre.Active(active: true);
				return;
			}
			Spectre.Active(active: false);
			Spectre.LookingAt(PlayingCharacter.gameObject.transform.position);
			PlayingCharacter.spectreThis = true;
		}
		else
		{
			Spectre = (SpectreCamera)UnityEngine.Object.FindObjectOfType(typeof(SpectreCamera));
			if (!(Spectre == null))
			{
			}
		}
	}

	private void findPlayerCharacter()
	{
		if (!(PlayingCharacter == null))
		{
			return;
		}
		CharacterSystem[] array = (CharacterSystem[])UnityEngine.Object.FindObjectsOfType(typeof(CharacterSystem));
		foreach (CharacterSystem characterSystem in array)
		{
			if ((bool)characterSystem && characterSystem.IsMine)
			{
				spectreMode = false;
				PlayingCharacter = characterSystem;
				if (SavePlayer)
				{
					PlayingCharacter.LoadCharacterData(SaveToServer);
				}
			}
		}
	}

	public Vector3 FindASpawnPoint(int spawner)
	{
		Vector3 result = Vector3.zero;
		PlayerSpawner[] array = (PlayerSpawner[])UnityEngine.Object.FindObjectsOfType(typeof(PlayerSpawner));
		if (spawner < 0 || spawner >= array.Length)
		{
			spawner = UnityEngine.Random.Range(0, array.Length);
		}
		if (array.Length > 0)
		{
			result = array[spawner].transform.position;
		}
		return result;
	}

	public GameObject InstantiatePlayer(int playerID, string userID, string userName, string characterKey, int characterIndex, byte team, int spawner)
	{
		if (UnitZ.characterManager == null || UnitZ.characterManager.CharacterPresets.Length <= characterIndex || characterIndex < 0)
		{
			return null;
		}
		CharacterSystem characterPrefab = UnitZ.characterManager.CharacterPresets[characterIndex].CharacterPrefab;
		if ((bool)characterPrefab)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(characterPrefab.gameObject, FindASpawnPoint(spawner), Quaternion.identity);
			CharacterSystem component = gameObject.GetComponent<CharacterSystem>();
			component.NetworkNetID = playerID;
			component.NetworkTeam = team;
			component.NetworkCharacterKey = characterKey;
			component.NetworkUserName = userName;
			component.NetworkUserID = userID;
			MouseLock.MouseLocked = true;
			return gameObject;
		}
		return null;
	}
}
